package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.supplier.entity.StatusEntity;
import com.epfresh.supplier.widget.*;
import com.epfresh.supplier.R;
import com.epfresh.supplier.dialog.CustomDetailDeleteDialog;
import com.epfresh.supplier.entity.CustomerItem;
import com.epfresh.supplier.entity.DeliveryItem;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class CustomerDetailActivity extends RequestDeliveryActivity implements ViewVariable {

    private TextView mTvDetailName;
    private TextView mTvDetailTel;
    private TextView mTvDetailCompany;
    private TextView mTvDetailGroup;
    private TextView mTvDetailAdressNo;
    private TextView mTvDetailRemark;
    private CustomerItem mDetailItem;
    private LinearLayout mLlGroups;
    private String mAccountId;
    private int mAdressNo;
    private CustomDetailDeleteDialog deleteDialog;
    private Button mBtnDelete;
    private Button mBtnEdit;
    private View ll_bottom;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_detail);
        initView();
        getCustomerDetailData();
    }

    @Override
    protected View getToolBar() {
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void getCustomerDetailData() {
        mDetailItem = getIntent().getParcelableExtra("customerItem");
        initDataView();
/*        if(mDetailItem.isTemp()){
            mBtnDelete.setEnabled(false);
            mBtnEdit.setEnabled(false);
        }else {
            mBtnDelete.setEnabled(true);
            mBtnEdit.setEnabled(true);
        }*/
    }

    private void initDataView() {
        mTvDetailName.setText(TextUtils.isEmpty(mDetailItem.getName()) ? "" : mDetailItem.getName());
        mTvDetailTel.setText(TextUtils.isEmpty(mDetailItem.getPhone()) ? "" : mDetailItem.getPhone());
        mTvDetailCompany.setText(TextUtils.isEmpty(mDetailItem.getOrg()) ? "无" : mDetailItem.getOrg());
        Log.e("groupSize",""+getIntent().getIntExtra("groupSize", 0));
        if (getIntent().getIntExtra("groupSize", 0) > 1) {
            mLlGroups.setVisibility(View.VISIBLE);
            mTvDetailGroup.setText(mDetailItem.getGroupName());
        } else {
            mLlGroups.setVisibility(View.GONE);
        }
        mAccountId = mDetailItem.getAccountId();
        //得到收获地址个数
        getDeliveryList(mAccountId);
        mTvDetailRemark.setText(TextUtils.isEmpty(mDetailItem.getRemark()) ? "无" : mDetailItem.getRemark());
    }


    private void initView() {
        ll_bottom = findViewById(R.id.ll_bottom);
        if (BaseApplication.getInstance().getMerchant().isAuthNeedBind()) {
            ll_bottom.setVisibility(View.GONE);
        } else {
            ll_bottom.setVisibility(View.VISIBLE);
        }

        mBtnDelete = (Button) findViewById(R.id.btn_detail_delete);
        mBtnEdit= (Button) findViewById(R.id.btn_detail_edit);
        mTvDetailName = (TextView) findViewById(R.id.tv_detail_name);
        mTvDetailTel = (TextView) findViewById(R.id.tv_detail_tel);
        mTvDetailCompany = (TextView) findViewById(R.id.tv_detail_company);
        mTvDetailGroup = (TextView) findViewById(R.id.tv_detail_group);
        mTvDetailAdressNo = (TextView) findViewById(R.id.tv_detail_adress_no);
        mTvDetailRemark = (TextView) findViewById(R.id.tv_detail_remark);
        mLlGroups = (LinearLayout) findViewById(R.id.ll_detail_choose_group);
        mLlGroups.setVisibility(View.GONE);
        findViewById(R.id.back_ll).setOnClickListener(this);
        mBtnEdit.setOnClickListener(this);
        findViewById(R.id.rl_detail_address).setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        Log.e("_______________",""+v.getId());
        switch (v.getId()) {
            case R.id.back_ll:
                finish();
                break;
            case R.id.btn_detail_edit:
                Intent edit = new Intent(this, CustomerEditAddActivity.class);
                edit.setAction(CUSTOMER_DETAIL_ACTIVITY);
                edit.putExtra("CustomerItem", mDetailItem);
                edit.putExtra("addressNo", String.valueOf(mAdressNo));
                edit.putExtra("groupSize",getIntent().getIntExtra("groupSize",0));

                startActivityForResult(edit,0x001);
                break;
/*            case R.id.rl_detail_address:
                Intent intent = new Intent(this, CustomerDeliveryActivity.class);
                intent.putExtra("accountId", mDetailItem.getAccountId());
                startActivity(intent);
                break;*/
            case R.id.btn_detail_delete:
                intoDeleteDialog();
                break;
        }
    }
    private void intoDeleteDialog() {
        deleteDialog = new CustomDetailDeleteDialog(this, true);
        deleteDialog.setMessage("确定删除该客户吗？");
        deleteDialog.setOnPositiveClickListener("确定", new CustomDetailDeleteDialog.onPositiveClickListener() {
            @Override
            public void onPositiveClickListener() {
                deleteDialog.dismiss();
                deleteCustomer(mDetailItem.getId());
            }
        });
        deleteDialog.setOnNegativeClickListener("取消", new CustomDetailDeleteDialog.onNegativeClickListener() {
            @Override
            public void onNegativeClickListener() {
                deleteDialog.dismiss();
            }
        });
        deleteDialog.show();

    }
    /**
     * 提交修改
     */
    private void deleteCustomer(String mId) {
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_account_customer_delete);
        Map<String, String> map = new HashMap<>();
        map.put("id", mId);
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_account_customer_delete, onRequestListener);
    }

    OnRequestListener<StatusEntity> onRequestListener = new OnRequestListener<StatusEntity>() {
        @Override
        public StatusEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, StatusEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {


        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
        }

        @Override
        public void onStart(Object tag) {

        }

        @Override
        public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {
            StatusEntity responseElement = response.getResponseElement();
            if (responseElement.isSuccess()) {
                toast("已删除");
                finish();
                return;
            }
            T.toast("修改出现异常，请稍后再试");
        }
    };

    @Override
    protected void updateUI(ArrayList<DeliveryItem> mDeliveryitem) {
        mAdressNo = mDeliveryitem.size();
        if(0 == mAdressNo){
            mTvDetailAdressNo.setText("无");
        }else {
            mTvDetailAdressNo.setText(String.valueOf(mDeliveryitem.size()) + "个");
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if(resultCode == 0x001){
            mDetailItem = data.getParcelableExtra("customerItem");
            initDataView();
        }
    }
}
